package com.qf.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    public static final String DELAYED_EXCHANGE = "delayed_exchange";
    public static final String BIZ_EXCHANGE = "biz_exchange";
    public static final String DEAD_EXCHANGE = "dead_exchange";

    public static final String BIZ_ROUTING_KEY = "#.rabbit";
    public static final String DEAD_ROUTING_KEY = "#.dog";

    public static final String BIZ_QUEUE = "biz_queue";
    public static final String DEAD_QUEUE = "dead_queue";
    public static final String DELAYED_QUEUE = "delayed_queue";


    // 创建延迟交换机
    @Bean
    public CustomExchange delayedExchange(){
        Map<String, Object> map = new HashMap<>();
        map.put("x-delayed-type","fanout");
        CustomExchange delayedExchange = new CustomExchange(DELAYED_EXCHANGE,"x-delayed-message",true,false,map);
        return delayedExchange;
    }

    // 延迟队列
    @Bean
    public Queue delayedQueue(){
        return QueueBuilder.durable(DELAYED_QUEUE).build();
    }


    // 绑定延迟交换机和队列
    @Bean
    public Binding delayedBinding(CustomExchange delayedExchange,Queue delayedQueue){
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with("").noargs();
    }














    // 创建业务交换机
    @Bean
    public Exchange bizExchange(){
        return ExchangeBuilder.topicExchange(BIZ_EXCHANGE).durable(true).build();
    }

    // 创建死信交换机
    @Bean
    public Exchange deadExchange(){
        return ExchangeBuilder.topicExchange(DEAD_EXCHANGE).durable(true).build();
    }


    // 创建业务队列
    @Bean
    public Queue bizQueue(){
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        args.put("x-dead-letter-routing-key","big.black.dog");
//        args.put("x-message-ttl",5000);
        return QueueBuilder.durable(BIZ_QUEUE).withArguments(args).build();
    }

    // 创建死信队列
    @Bean
    public Queue deadQueue(){
        return QueueBuilder.durable(DEAD_QUEUE).build();
    }

    // 业务绑定
    @Bean
    public Binding bizBinding(Queue bizQueue,Exchange bizExchange){
        return BindingBuilder.bind(bizQueue).to(bizExchange).with(BIZ_ROUTING_KEY).noargs();
    }


    // 死信绑定
    @Bean
    public Binding deadBinding(Queue deadQueue,Exchange deadExchange){
        return BindingBuilder.bind(deadQueue).to(deadExchange).with(DEAD_ROUTING_KEY).noargs();
    }


}
